package svg

import (
	"strings"
	
	"gosvg/dom"
)

type textNode struct {
	str string
}

func (tn *textNode) XmlWrite(writer *dom.Writer) {
	val := tn.str
	if k := strings.TrimLeft(val, " \n\t\r"); k != val {
		val = " " + k
	}

	if k := strings.TrimRight(val, " \n\t\r"); k != val {
		val = k + " "
	}

	writer.IndentPrint(val)
}

type TextElement struct {
	*dom.Element
}

func Text() *TextElement {
	return &TextElement{Element: dom.MakeElementNs("text", SvgNamespace)}
}

func (t *TextElement) Write(s string) {
	t.Element.Child(&textNode{str: s})
}

//X sets the new absolute X coordinate for the current text position for 
//rendering the glyphs that correspond to the first character within this 
//element or any of its descendants.
func (r *TextElement) X(x float64) {
	r.Element.SetAttribute("x", fVal(x))
}

//Xs sets the new absolute X coordinates for the current text position 
//for rendering the glyphs corresponding to each of the first n characters 
//within this element or any of its descendants.
func (r *TextElement) Xs(xs []float64) {
	if len(xs) < 1 {
		return
	}

	r.Element.SetAttribute("x", fVal(xs[0]))
	for _, v := range xs {
		r.Element.AddToAttribute("x", fVal(v))
	}
}

//Y sets the new absolute Y coordinate for the current text position for 
//rendering the glyphs that correspond to the first character within this 
//element or any of its descendants.
func (r *TextElement) Y(y float64) {
	r.Element.SetAttribute("y", fVal(y))
}

//Xs sets the new absolute Y coordinates for the current text position 
//for rendering the glyphs corresponding to each of the first n characters 
//within this element or any of its descendants.
func (r *TextElement) Ys(ys []float64) {
	if len(ys) < 1 {
		return
	}

	r.Element.SetAttribute("y", fVal(ys[0]))
	for _, v := range ys {
		r.Element.AddToAttribute("x", fVal(v))
	}
}
